package com.hjx.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by houjinxin on 16-12-30.
 */
public class ResourcesUtil {

    private static final Logger logger = LoggerFactory.getLogger(ResourcesUtil.class);
    private static final ResourceBundle resourceBundle = ResourceBundle.getBundle("config");

    public static String getString(String key) {
        return resourceBundle.getString(key);
    }

//    public static String getFlowFilePathByName(String flowFileName) {
//        ClassLoader classPathLoader = ResourcesUtil.class.getClassLoader();
//        URL classpathUrl = classPathLoader.getResource("");
//        if (classpathUrl == null){
//            throw new RuntimeException("classpath路径错误");
//        }
//        String classpath = classpathUrl.getPath();
//        String flowFilePath = classpath + resourceBundle.getString(flowFileName);
//        if (flowFilePath.contains("test-classes")){ //test-classpath
//            URL flowFileUrl = classPathLoader.getResource(resourceBundle.getString(flowFileName));
//            if(!new File(flowFileUrl.getPath()).exists()) { //test-classpath不存在，去classpath找
//                logger.info("test-classpath找不到流程定义文件，去classpath下寻找");
//                flowFilePath = flowFilePath.replace("test-classes", "classes");
//                if(new File(flowFilePath).exists()){
//                    logger.info("找到流程定义文件，路径为：" + flowFilePath);
//                    return flowFilePath;
//                } else {
//                    throw new RuntimeException("classpath下不存在流程定义文件，文件路径为：" + flowFilePath);
//                }
//            }
//            logger.info("找到流程定义文件，路径为：" + flowFilePath);
//            return flowFilePath;
//        } else { //classpath
//            if(new File(flowFilePath).exists()){
//                logger.info("找到流程定义文件，路径为：" + flowFilePath);
//                return flowFilePath;
//            } else {
//                throw new RuntimeException("classpath下不存在流程定义文件，文件路径为：" + flowFilePath);
//            }
//        }
//    }

    public static String getFlowFilePathByName(String flowFileName) {
        ClassLoader classLoader = ResourcesUtil.class.getClassLoader();
        URL flowFileUrl = classLoader.getResource(resourceBundle.getString(flowFileName));
        String flowFilePath = flowFileUrl.getPath();
        if(new File(flowFilePath).exists()) {
            logger.info("找到流程定义文件，路径为：{}", flowFilePath);
            return flowFilePath;
        } else {
            throw new RuntimeException("classpath下不存在流程定义文件，文件路径为：" + flowFilePath);
        }

    }

    public static String getPropertyByName(String propertyName) {
        ClassLoader classLoader = ResourcesUtil.class.getClassLoader();
        URL flowFileUrl = classLoader.getResource(resourceBundle.getString(propertyName));
        String flowFilePath = flowFileUrl.getPath();
        if(new File(flowFilePath).exists()) {
            logger.info("找到目标文件，路径为：{}", flowFilePath);
            return flowFilePath;
        } else {
            throw new RuntimeException("classpath下不存在流程定义文件，文件路径为：" + flowFilePath);
        }

    }

}
